package com.icom.sso.mapper;

import java.util.List;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import com.xxl.sso.server.core.model.BackTree;
import com.xxl.sso.server.core.model.Department;

@Mapper
public interface DepartmentMapper {

	/**
	 * 按条件进行查询结果，（id为空时查询所有，不为空时查询他及他的所有子节点）
	 * 
	 * @param dptId
	 * @return
	 */
	@Select({ "<script>", "select * from ", "<choose><when test='dptId!=null'> ",
			" (select *,if(find_in_set(pid, @pids) > 0, @pids := concat(@pids, ',', id), 0) as ischild from (",
			"select * from sys_department t where t.status = 1 ",
			" ) t1,(select @pids := #{dptId}) t2) t3 where status = 1 and ischild != 0 ", "</when><otherwise>",
			" sys_department where status = 1 ", "</otherwise></choose>", "<when test='name!=null'> ",
			"and dpt_name like '%${name}%'", "</when>", "<when test='no!=null'> ", "and dpt_no like '%${no}%'",
			"</when>", "order by level,dpt_no", "</script>" })
	@Results({ @Result(id = true, column = "id", property = "id"), @Result(column = "dpt_no", property = "deptNo"),
			@Result(column = "dpt_name", property = "name"), @Result(column = "level", property = "level"),
			@Result(column = "islast", property = "isLast"), @Result(column = "status", property = "status"),
			@Result(column = "pid", property = "pid"), @Result(column = "createdby", property = "createdby"),
			@Result(column = "createdon", property = "createdon"),
			@Result(column = "modifiedby", property = "modifiedby"),
			@Result(column = "modifiedon", property = "modifiedon") })
	List<Department> findDepts(@Param(value = "dptId") String dptId, @Param(value = "name") String name,
			@Param(value = "no") String no);

	@Select("select id,pid,dpt_name,level from sys_department where status = 1 order by level")
	@Results({ @Result(id = true, column = "id", property = "id"), @Result(column = "dpt_name", property = "text"),
			@Result(column = "level", property = "level"), @Result(column = "pid", property = "parent") })
	List<BackTree> findDeptTree();

	@Select({ "<script>", "select * from sys_department where status = 1 and id = #{id}", "</script>" })
	@Results({ @Result(id = true, column = "id", property = "id"), @Result(column = "dpt_no", property = "deptNo"),
			@Result(column = "dpt_name", property = "name"), @Result(column = "level", property = "level"),
			@Result(column = "islast", property = "isLast"), @Result(column = "status", property = "status"),
			@Result(column = "pid", property = "pid"), @Result(column = "createdby", property = "createdby"),
			@Result(column = "createdon", property = "createdon"),
			@Result(column = "modifiedby", property = "modifiedby"),
			@Result(column = "modifiedon", property = "modifiedon") })
	Department findDeptById(@Param(value = "id") String id);

	@Insert("insert into sys_department(dpt_no, dpt_name, status, level, islast, pid, createdby, createdon, modifiedby, modifiedon) "
			+ "values(#{deptNo}, #{name}, #{status},#{level}, #{isLast}, #{pid},#{createdby}, #{createdon}, #{modifiedby}, #{modifiedon})")
	int saveDept(Department department);

	@Update("update sys_department set dpt_name = #{name},islast=#{isLast},status=#{status} where id = #{id}")
	int saveEditInfo(Department department);

	@Update("update sys_department set status = 0 where id = #{id}")
	int delDept(@Param("id") String id);

	@Select("select * from sys_department where status = 1")
	@Results({ @Result(id = true, column = "id", property = "id"), @Result(column = "dpt_no", property = "deptNo"),
			@Result(column = "dpt_name", property = "name"), @Result(column = "level", property = "level"),
			@Result(column = "islast", property = "isLast"), @Result(column = "status", property = "status"),
			@Result(column = "pid", property = "pid"), @Result(column = "createdby", property = "createdby"),
			@Result(column = "createdon", property = "createdon"),
			@Result(column = "modifiedby", property = "modifiedby"),
			@Result(column = "modifiedon", property = "modifiedon") })
	List<Department> findDepartments();
}
